import com from './addon/com'
import urly from './addon/urly'

var components = com([
	'HeaderLayout',
	'HeaderDock',
	'LayoutMain',
	'LayoutBox',
	'LayoutSideMenu',
	'TabsSide',
	'TabsItemSide',
	'Icon',
	'List',
	'ListItem',
	'Product',
	'ShoppingDock',
	'FormStars',
	'FormCheck',
	'GridRow',
	'GridCol',
	'Btn'
])

//cvx
new Vue({
	el: '#app',
	components: components,
	data (){
		return {
			 currentDate: '',
			 currentDay: '',
			 total: TOTAL,
			 week: ['一', '二', '三', '四', '五', '六', '日'],
			 comments: [],
			commentsPageSize: 10,
			commentsRequest: null,
			checked: CHECKED,
			commentShow: false,
			commentcss: ''
		}
	},
	ready: function(){
		var _this = this;
		var url = new urly(window.location.href);
		//this.currentDate = url.getParams().date;
		this.currentDate = DATE;
		var dd = DATE.split('-');
		var d = new Date(dd[0], dd[1]-1, dd[2], '', '', '');

		this.currentDay = d.getDay() -1;
 
		this.loadingComments({
			pageSize: this.commentsPageSize,
			page: 0
		});

	},
	events: {

	},
	methods: {
		gocomment: function(){
			this.commentShow = true;
		},
		cancelcomment: function(){
			this.commentShow = false;
		},
		focuscomment: function(){
			this.commentcss = 'type'
		},
		blurcomment: function(){
			this.commentcss = ''
		},
		loadingComments: function(param){
			var _this = this;
			_this.$refs.comments.setLoading();

			if(_this.commentsRequest){
				_this.commentsRequest.abort();
			}
			_this.commentsRequest = $.ajax({
				url: SITE_URL + '/home/get_productcomment',
				type: 'get',
				data: {
					productid: PRODUCT.id,
					page: param.pageSize,
					offset: param.pageSize*param.page
				},
				success: function(data){
					_this.$refs.comments.addData(data, 'id');
					//_this.comments = _this.$refs.comments.getData();
					var comments = _this.$refs.comments.getData();
					for(var key=0; key<comments.length;key++){
						if(comments[key].obj.score == 0){
							comments[key].show = false
						} else {
							comments[key].show = true;
						}
					}

					_this.comments = comments;


					_this.$refs.comments.reSet();
					if(_this.comments.length == 0){
						_this.$refs.comments.setEnd('没有评价');
					}
					if(data.length < _this.commentsPageSize){
						_this.$refs.comments.setEnd();
					}
				},
				error: function(){

				}
			});
		},
		 buy: function(check){

		 	if(!check.checked){
		 		check.setChecked(true);
		 		return;
		 	}
		 	var _this = this;
		 	$.ajax({
				url: SITE_URL + '/shoppingcart/addtocart',
				data: {
					productid: PRODUCT.id,
					amount: 1,
					date: _this.currentDate
				},
				type: 'post',
				success: function(data){
					if(data.code){
						_this.total = data.data;
					} else {
						check.setChecked(false);
						bone.dialog.init('速派友情提醒', data.msg).ok(function(){
							return true;
						})
					}

				},
				error: function(){
					//alert('网络错误，请重试');
				}

			});
		 },
		gotoshp: function(){
			window.location = SITE_URL + '/shoppingcart'
		},
		comment: function(){
			var comment = $('#comment').val();

	  		if(comment == ''){
	  			alert('评价不能为空');
	  			return;
	  		}

	  		var data = {
	  			productid: PRODUCT.id,
	  			content: comment,
	  			score: 0
	  		}


	  		$.ajax({
				url: SITE_URL + '/order/savecomment/',
				type: 'post',
				data: data,
				success: function(data){
					if(!data.code){
						alert(data.msg);
						//window.location.refresh();
					} else {
						alert(data.msg);
						window.location.reload();
					}

				},
				error: function(){
					alert('网络发生错误，请重试');
				}
			});
		}
	}
})
